﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Socium.Domain.Persons {
	[Serializable]
	public class Group {
		readonly ObservableCollection<Group> _groups = new ObservableCollection<Group>();
		readonly ICollection<Person> _persons = new LinkedList<Person>();

		public Group() {
			_groups.CollectionChanged += OnGroupsCollectionChanged;
		}

		void OnGroupsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
			if (e.NewItems != null) {
				foreach (Group new_item in e.NewItems) {
					new_item.Parent = this;
				}
			}

			if (e.OldItems != null) {
				foreach (Group old_item in e.OldItems) {
					old_item.Parent = null;
				}
			}
		}
		
		public string Name { get; set; }
		public string Description { get; set; }

		public Group Parent { get; private set; }

		public ICollection<Group> Groups {
			get { return _groups; }
		}

		public ICollection<Person> Persons {
			get { return _persons; }
		}

		public override string ToString() {
			return string.IsNullOrEmpty(Name) ? "Group" : Name;
		}
	}
}
